package com.rayboot.footpoint.activity;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import com.rayboot.footpoint.R;
import com.rayboot.footpoint.commonui.MapPhotoPinDrawable;
import com.rayboot.footpoint.model.Footprint;
import com.rayboot.footpoint.utils.Util;

public class ShowFootporinsOnMapActivity extends MapActivity {
	private MapView map = null;
	private MyLocationOverlay me = null;
	private SitesOverlay sites = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_footporints_on_map);

		map = (MapView) findViewById(R.id.mvSFMap);

		map.getController().setCenter(
				getPoint(40.748963847316034, -73.96807193756104));
		map.getController().setZoom(17);
		map.setBuiltInZoomControls(true);

		me = new MyLocationOverlay(this, map);
		map.getOverlays().add(me);
		new OverlayTask().execute();
	}

	@Override
	public void onResume() {
		super.onResume();

		me.enableCompass();
	}

	@Override
	public void onPause() {
		super.onPause();

		me.disableCompass();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return (false);
	}

	private GeoPoint getPoint(double lat, double lon) {
		return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
	}

	private class SitesOverlay extends ItemizedOverlay<CustomItem> {
		// private Drawable heart = null;
		private List<CustomItem> items = new ArrayList<CustomItem>();

		public SitesOverlay() {
			super(null);

			// heart = getMarker(R.drawable.heart_full);

			items.add(new CustomItem(getPoint(40.748963847316034,
					-73.96807193756104), "UN", "United Nations", getMarker(0),
					100));
			populate();
		}

		@Override
		protected CustomItem createItem(int i) {
			return (items.get(i));
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

		}

		@Override
		protected boolean onTap(int i) {
			CustomItem item = getItem(i);
			GeoPoint geo = item.getPoint();
			Point pt = map.getProjection().toPixels(geo, null);
			Toast.makeText(ShowFootporinsOnMapActivity.this, "1111",
					Toast.LENGTH_SHORT).show();

			// View view = panel.getView();
			//
			// ((TextView) view.findViewById(R.id.latitude)).setText(String
			// .valueOf(geo.getLatitudeE6() / 1000000.0));
			// ((TextView) view.findViewById(R.id.longitude)).setText(String
			// .valueOf(geo.getLongitudeE6() / 1000000.0));
			// ((TextView) view.findViewById(R.id.x))
			// .setText(String.valueOf(pt.x));
			// ((TextView) view.findViewById(R.id.y))
			// .setText(String.valueOf(pt.y));
			//
			// panel.show(pt.y * 2 > map.getHeight());

			return (true);
		}

		@Override
		public int size() {
			return (items.size());
		}

		private Drawable getMarker(int resource) {
			Drawable marker = new MapPhotoPinDrawable(getResources()
					.getDrawable(R.drawable.red),
					Util.boundCenterBottom(getResources().getDrawable(
							R.drawable.footprint_tbar_single_normal)),
					Util.boundCenterBottom(getResources().getDrawable(
							R.drawable.footprint_tbar_single_pressed)), true);
			return (marker);
		}
	}

	class CustomItem extends OverlayItem {
		Drawable marker = null;
		int tag = -1;

		CustomItem(GeoPoint pt, String name, String snippet, Drawable marker,
				int tag) {
			super(pt, name, snippet);
			this.tag = tag;
			this.marker = marker;
		}

		@Override
		public Drawable getMarker(int stateBitset) {
			Drawable result = marker;

			setState(marker, stateBitset);

			return (result);
		}

	}

	class OverlayTask extends AsyncTask<Void, Void, Void> {
		@Override
		public void onPreExecute() {
			if (sites != null) {
				map.getOverlays().remove(sites);
				map.invalidate();
				sites = null;
			}
		}

		@Override
		public Void doInBackground(Void... unused) {
			// SystemClock.sleep(5000); // simulated work
			sites = new SitesOverlay();
			return (null);
		}

		@Override
		public void onPostExecute(Void unused) {
			map.getOverlays().add(sites);
			map.invalidate();
		}
	}

}
